$(function () {
    //1. ajax获取文章列表
    initArtCateList();
    function initArtCateList() {
        axios({
            url: '/my/article/cates',
        }).then(res => {
            // console.log(res.data);
            if (res.data.status !== 0) {
                return layui.layer.msg(res.data.message);
            }
            //渲染页面
            randerList(res.data.data);
        })
    }

    function randerList(resData) {
        let ary = [];
        resData.forEach(ele => {
            ary.push(`
                    <tr>
                        <td>${ele.Id}</td>
                        <td>${ele.name}</td>
                        <td>${ele.alias}</td>
                        <td>
                            <button data-id = ${ele.Id} class="btn-edit layui-btn layui-btn-xs">修改</button>
                            <button id="btn-del" data-id=${ele.Id} class="layui-btn layui-btn-xs layui-btn-danger ">删除</button>
                        </td>
                    </tr>
            `);
        });
        $('tbody').empty().html(ary.join(''));
    }

    //2. 弹出层对话框 添加分类
    let index = 0;
    $('#addBtn').on('click', function () {
        index = layer.open({
            type: 1,
            area: ['500px', '260px'],
            title: '添加文章分类',
            content: `
                    <form id="addForm" class="layui-form" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类名称</label>
                            <div class="layui-input-block">
                                <input id="aaa" type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类别名</label>
                            <div class="layui-input-block">
                                <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button id="btn-add" class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </form>  
            `
        });

        $('#aaa').focus();
    })

    //3. 添加文章分类
    $('body').on('submit', '#addForm', function (e) {
        e.preventDefault();
        // alert(123)
        //添加文章分类
        axios({
            method: 'POST',
            url: '/my/article/addcates',
            data: $('#addForm').serialize(),
        }).then(res => {
            // console.log(res.data)
            if (res.data.status !== 0) {
                return layui.layer.msg(res.data.message)
            };
            layui.layer.msg('恭喜添加分类成功')
            initArtCateList();
            // 正如你看到的，每一种弹层调用方式，都会返回一个index
            layer.close(index); //此时你只需要把获得的index，轻轻地赋予layer.close即可
        })
    })

    //4.点击修改按钮弹出对话框并赋值
    let indexEdit = 0;
    $('tbody').on('click', '.btn-edit', function () {
        // alert(123)
        indexEdit = layer.open({
            type: 1,
            area: ['500px', '260px'],
            title: '修改文章分类',
            content: `
                    <form lay-filter="editForm" id="editForm" class="layui-form"  >
                        <input type="hidden" name="Id">
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类别名</label>
                            <div class="layui-input-block">
                                <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                            <button class="layui-btn" lay-submit>立即修改</button>
                            </div>
                        </div>
                    </form>  
                     `
        });
        //获取id
        let id = $(this).attr('data-id');
        // console.log(id);
        axios({
            method: 'get',
            url: '/my/article/cates/' + id,
        }).then(res => {
            // console.log(res.data);
            if (res.data.status !== 0) {
                return layui.layer.msg(res.data.message);
            };
            // layui.layer.msg('恭喜获取成功')
            layui.form.val("editForm", res.data.data);
        })
    })

    //5.修改
    $('body').on('submit', '#editForm', function (e) {
        e.preventDefault();
        // alert(123)
        //添加文章分类
        axios({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $('#editForm').serialize(),
        }).then(res => {
            // console.log(res.data)
            if (res.data.status !== 0) {
                return layui.layer.msg(res.data.message)
            };
            layui.layer.msg('恭喜添加分类成功')
            initArtCateList();
            // 正如你看到的，每一种弹层调用方式，都会返回一个index
            layer.close(indexEdit); //此时你只需要把获得的index，轻轻地赋予layer.close就会关闭对话框
        })
    })

    //6.删除
    $('body').on('click', '#btn-del', function () {
        let id = $(this).attr('data-id');
        layer.confirm('是否确认删除?', { icon: 3, title: '提示' }, function (index) {

            axios({
                url: '/my/article/deletecate/'+id,
            }).then(res => {
                // console.log(res.data);
                if(res.data.status !== 0){
                    return layui.layer.msg(res.data.message);
                }
                initArtCateList();
            });
            layer.close(index);
        });
    })
})